package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkTicketRefundAddBo;
import com.ruoyi.system.bo.ZsyyParkTicketRefundQueryBo;
import com.ruoyi.system.bo.ZsyyParkTicketRefundEditBo;
import com.ruoyi.system.domain.ZsyyParkTicketRefund;
import com.ruoyi.system.mapper.ZsyyParkTicketRefundMapper;
import com.ruoyi.system.vo.ZsyyParkTicketRefundVo;
import com.ruoyi.system.service.IZsyyParkTicketRefundService;
import com.ruoyi.system.mapper.ZsyyParkTicketRefundMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 取消月票记录Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkTicketRefundServiceImpl extends ServicePlusImpl<ZsyyParkTicketRefundMapper, ZsyyParkTicketRefund> implements IZsyyParkTicketRefundService {
    @Autowired
    private ZsyyParkTicketRefundMapper iZsyyParkTicketRefundMapper;

    @Override
    public ZsyyParkTicketRefundVo queryById(Long id){
        return getVoById(id, ZsyyParkTicketRefundVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkTicketRefundVo> queryPageList(ZsyyParkTicketRefundQueryBo bo) {
        PagePlus<ZsyyParkTicketRefund, ZsyyParkTicketRefundVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkTicketRefundVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkTicketRefundVo> queryList(ZsyyParkTicketRefundQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkTicketRefundVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkTicketRefund> buildQueryWrapper(ZsyyParkTicketRefundQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkTicketRefund> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getParkCode()), ZsyyParkTicketRefund::getParkCode, bo.getParkCode());
        lqw.eq(StrUtil.isNotBlank(bo.getBillCode()), ZsyyParkTicketRefund::getBillCode, bo.getBillCode());
        lqw.eq(StrUtil.isNotBlank(bo.getCarNo()), ZsyyParkTicketRefund::getCarNo, bo.getCarNo());
        lqw.eq(bo.getMonthTicketId() != null, ZsyyParkTicketRefund::getMonthTicketId, bo.getMonthTicketId());
        lqw.eq(bo.getRefundPrice() != null, ZsyyParkTicketRefund::getRefundPrice, bo.getRefundPrice());
        lqw.eq(bo.getPayOrigin() != null, ZsyyParkTicketRefund::getPayOrigin, bo.getPayOrigin());
        lqw.eq(StrUtil.isNotBlank(bo.getPayOriginRemark()), ZsyyParkTicketRefund::getPayOriginRemark, bo.getPayOriginRemark());
        lqw.eq(bo.getPayMode() != null, ZsyyParkTicketRefund::getPayMode, bo.getPayMode());
        lqw.eq(StrUtil.isNotBlank(bo.getPayModeRemark()), ZsyyParkTicketRefund::getPayModeRemark, bo.getPayModeRemark());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkTicketRefundAddBo bo) {
        ZsyyParkTicketRefund add = BeanUtil.toBean(bo, ZsyyParkTicketRefund.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkTicketRefundEditBo bo) {
        ZsyyParkTicketRefund update = BeanUtil.toBean(bo, ZsyyParkTicketRefund.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkTicketRefund entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
